package com.aegean.MonitorExamSYS.conduction.action;

import java.io.File;
import java.io.FileInputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.aspectj.util.FileUtil;
import org.hibernate.Hibernate;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.aegean.MonitorExamSYS.conduction.business.ConductionBusiness;
import com.aegean.MonitorExamSYS.examinee.business.ExamineeBusiness;
import com.aegean.MonitorExaxSYS.model.ConductionValue;
import com.aegean.MonitorExaxSYS.model.ExamineeValue;
import com.aegean.MonitorExaxSYS.model.component.Photo;
import com.opensymphony.xwork2.ActionSupport;

@Controller("registerConduction")
@Scope("prototype")
public class registerConduction extends ActionSupport {
	@Resource
	ConductionBusiness business;
	private ConductionValue conduction;
	private File photo;
	private String photoFileName;
	private String photoContentType;
	private boolean success;
	private String msg;

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public ConductionValue getConduction() {
		return conduction;
	}

	public void setConduction(ConductionValue conduction) {
		this.conduction = conduction;
	}

	public File getPhoto() {
		return photo;
	}

	public void setPhoto(File photo) {
		this.photo = photo;
	}

	public String getPhotoFileName() {
		return photoFileName;
	}

	public void setPhotoFileName(String photoFileName) {
		this.photoFileName = photoFileName;
	}

	public String getPhotoContentType() {
		return photoContentType;
	}

	public void setPhotoContentType(String photoContentType) {
		this.photoContentType = photoContentType;
	}

	@Override
	public String execute() throws Exception {
		try {
			String realPath = ServletActionContext.getServletContext()
					.getRealPath(
							"/dateFile/conduction/"
									+ conduction.getConduction_Name());
			File file_Small = new File(realPath);
			if (!file_Small.exists()) {
				file_Small.mkdir();
			}
			File file_Photo = new File(realPath, photoFileName);
			FileUtil.copyFile(photo, file_Photo);
			Photo sphoto = new Photo();
			sphoto.setPhoto(null);
			sphoto.setPhoto_Name(photoFileName);
			sphoto.setPhoto_Type(photoContentType);
			conduction.setConduction_Photo(sphoto);
			conduction = business.insertConduction(conduction);
			if (conduction != null && conduction.getConduction_Id() != -1) {
				success = true;
				msg = "恭喜您注册成功，请’" + conduction.getConduction_Name()
						+ "‘贵单位关闭此网页，进入登陆窗口，进行相关业务！";
				HttpSession session=ServletActionContext.getRequest().getSession();
				session.setAttribute("ConductionLogin",conduction);
			} else {
				success=false;
				msg="对不起，注册失败，请联系考试中心，谢谢您的支持！";
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			success=false;
			msg="对不起，注册失败，请联系考试中心，谢谢您的支持！";
			return "success";
		}

	}
}
